package com.victor.controller.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.poi.PoiSubmit;
import com.victor.service.PoisubmitService;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/poi")
public class PoiController {
	@Autowired
	private PoisubmitService poisubmitService;
	
	/**
	 * 查询poi列表
	 * @param page
	 * @param row
	 * @param aflag
	 * @return
	 */
	@RequestMapping("/getall")
	public Msg findAllPoi(int page,int row,String search,String flag) {
		PageInfo<PoiSubmit> selAllOrSearchOrFlag = poisubmitService.selAllOrSearchOrFlag(row,page,search,flag);
		return Msg.success().add("info", selAllOrSearchOrFlag);
	}
	/**
	 * 查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/xiangqing")
	public Msg Xiangqing(Integer id) {
		if (id == null) {
			return Msg.fail("入参错误");
		}
		PoiSubmit poiSubmit = poisubmitService.selById(id);
		return Msg.success().add("poiSubmit", poiSubmit);
	}
	/**
	 * 通过 拒绝
	 * @param poiSubmit
	 * @return
	 */
	@RequestMapping("/update")
	public Msg updateFlag(PoiSubmit poiSubmit) {
		if (poiSubmit.getId()== null || poiSubmit.getFlag() == null || poiSubmit.getFlag().isEmpty()) {
			return Msg.fail("入参失败");
		}
		if (poiSubmit.getFlag()=="1" || poiSubmit.getFlag().equals("1")) {
			poiSubmit.setFlag("1");
			boolean b = poisubmitService.update(poiSubmit);
			if (!b) {
				return Msg.fail("修改poi状态为 景区已审核 失败");
			}
		}
		if (poiSubmit.getFlag()=="2" || poiSubmit.getFlag().equals("2")) {
			poiSubmit.setFlag("2");
			boolean b = poisubmitService.update(poiSubmit);
			if (!b) {
				return Msg.fail("修改poi状态为 景区拒绝 失败");
			}
		}
		if (poiSubmit.getFlag()=="3" || poiSubmit.getFlag().equals("3")) {
			poiSubmit.setFlag("3");
			boolean b = poisubmitService.update(poiSubmit);
			if (!b) {
				return Msg.fail("修改poi状态为 管理员已审核 失败");
			}
		}
		if (poiSubmit.getFlag()=="4" || poiSubmit.getFlag().equals("4")) {
			poiSubmit.setFlag("4");
			boolean b = poisubmitService.update(poiSubmit);
			if (!b) {
				return Msg.fail("修改poi状态为  管理员拒绝 失败");
			}
		}
		return Msg.success();
		
	}
	
	@RequestMapping("/export")
	public void export(HttpServletRequest request,HttpServletResponse response,String adminid) throws Exception {
		List<PoiSubmit> list = poisubmitService.selAll();
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		Sheet sheet = sxssfWorkbook.createSheet("poi报送列表");
		CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 1, (short)0, (short)6);
		sheet.addMergedRegion(cellRangeAddress);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("poi报送列表");
		
		Row headrow = sheet.createRow(4);
		headrow.createCell(0).setCellValue("序号");
		headrow.createCell(1).setCellValue("日期");
		headrow.createCell(2).setCellValue("景区名称");
		headrow.createCell(3).setCellValue("poi名称");
		headrow.createCell(4).setCellValue("北纬");
		headrow.createCell(5).setCellValue("东经");
		
		for(PoiSubmit poiSubmit : list) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum()+1);
			if(poiSubmit.getId() == null) {
				dataRow.createCell(0).setCellValue("");
			}else {
				dataRow.createCell(0).setCellValue(poiSubmit.getId());
			}
			
			if(poiSubmit.getTime()== null) {
				dataRow.createCell(1).setCellValue("");
			}else {
				dataRow.createCell(1).setCellValue(poiSubmit.getTime());
			}
			if(poiSubmit.getName() == null) {
				dataRow.createCell(2).setCellValue("");
			}else {
				dataRow.createCell(2).setCellValue(poiSubmit.getName());
			}
//			if(poiSubmit.getA_label()== null) {
//				dataRow.createCell(3).setCellValue("");
//			}else {
//				dataRow.createCell(3).setCellValue(poiSubmit.getA_label());
//			}
//			if(poiSubmit.getA_photo() == null) {
//				dataRow.createCell(4).setCellValue("");
//			}else {
//				dataRow.createCell(4).setCellValue(poiSubmit.getA_photo());
//			}
//			if(poiSubmit.getA_username() == null) {
//				dataRow.createCell(5).setCellValue("");
//			}else {
//				dataRow.createCell(5).setCellValue(poiSubmit.getA_username());
//			}
//			if(poiSubmit.getA_password() == null) {
//				dataRow.createCell(6).setCellValue("");
//			}else {
//				dataRow.createCell(6).setCellValue(poiSubmit.getA_password());
//			}
		}
		String filename = "poi报送列表";
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		ServletOutputStream outputStream = response.getOutputStream();
		sxssfWorkbook.write(outputStream);
		outputStream.close();
		sxssfWorkbook.close();
		//logcontroller.addLog(adminid, "导出了管理员列表");
	}

}
